<?php
require_once('../inc/connect.php');
$section = 'User';

$submit = (isset($_POST['submit']) ? $_POST['submit'] : null);

$error;
if(isset($submit))
{
    $email =  strtolower(strip_tags($_POST['email']));
    $password = strip_tags($_POST['password']);
    $repassword = strip_tags($_POST['repassword']);
    $fname = strip_tags($_POST['first_name']);
    $mi = strip_tags($_POST['middle_initial']);
    $lname = strip_tags($_POST['last_name']);
    $created = date("Y-m-d H:i:s");

    $connect = mysql_connect(DB_HOST, DB_USER, DB_PASS);
    mysql_select_db(DB_NAME);
    
    $emailcheck = mysql_query("SELECT email FROM users WHERE email='$email'");
    $count = mysql_num_rows($emailcheck);
    
    if($count != 0)
    {
        die("Email already registered!");
    }
    
    if(isset($email) && isset($password) && isset($repassword) &&
       isset($fname) && isset($lname))
    {
        if($password == $repassword)
        {
            if(strlen($fname) > 64 || strlen($lname) > 64)
            {
                $error = "Length of first or last name is too long!";
            }
            else
            {
                if(strlen($password) < 6 || strlen($password) > 24)
                {
                    $error = "Password must be between 6 and 24 characters!";
                }
                else
                {
                    $password = md5($password);
                    $repassword = md5($repassword);
                    
                    $queryreg = mysql_query("INSERT INTO users (created, email, password, first_name, last_name) VALUES(NOW(), '$email', '$password', '$fname','$lname')");
                    
                    die('You have been registered! <a href="index.php">Return to index.</a>');
                }
            }
        }
        else
            $error = "Passwords do not match!";
    }
    else
        $error = "Please fill in all fields!";
}
else
{
    $email =  null;
    $password = null;
    $repassword = null;
    $fname = null;
    $mi = null;
    $lname = null;
}

?>
<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />
    <title>Register</title>
    <link rel="stylesheet" type="text/css" href="../css/reset.css" />
    <link rel="stylesheet" type="text/css" href="../css/style.css" />
  </head>

  <body>
<?php include('../inc/nav.php'); ?>
<?php include('../inc/header.php'); ?>
    <div id="content">
<?php if(isset($error)): ?>
      <p><?php echo $error; ?></p>
<?php endif; ?>
      <form action="register.php" method="POST">
        <table>
          <tbody>
            <tr>
              <td>Email</td>
              <td>
                <input type="text" name="email" maxlength="127"
                value="<?php echo $email; ?>" />
              </td>
            </tr>
            <tr>
              <td>Password</td>
              <td>
                <input type="password" name="password" maxlength="64" />
              </td>
            </tr>
            <tr>
              <td>Repeat password</td>
              <td>
                <input type="password" name="repassword" maxlength="64" />
              </td>
            </tr>
            <tr>
              <td>First Name</td>
              <td>
                <input type="text" name="first_name" maxlength="64"
                value="<?php echo $fname; ?>" />
              </td>
            </tr>
            <tr>
              <td>Middle Initial</td>
              <td>
                <input type="text" name="middle_initial" maxlength="1"
                value="<?php echo $mi; ?>" />
              </td>
            </tr>
            <tr>
              <td>Last Name</td>
              <td>
                <input type="text" name="last_name" maxlength="64"
                value="<?php echo $lname; ?>" />
              </td>
            </tr>
          </tbody>
          <tfoot>
            <tr>
              <td colspan="2">
                <input type="submit" name="submit" value="Register" />
              </td>
            </tr>
          </tfoot>
        </table>
      </form>
    </div><!-- #content -->
<?php include('../inc/footer.php'); ?>
  </body>
</html>
